package com.wenda.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wenda.admin.bean.Account;
import com.wenda.admin.bean.Student;
import com.wenda.admin.constant.StatusEnum;
import com.wenda.admin.constant.UserTypeEnum;
import com.wenda.admin.constant.YesNoEnum;
import com.wenda.admin.mapper.AccountMapper;
import com.wenda.admin.mapper.StudentMapper;
import com.wenda.admin.utils.CodeUtil;
import com.wenda.admin.vo.QueryVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;


@Controller
@RequestMapping("student")
public class StudentController {
    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private AccountMapper accountMapper;

    @RequestMapping("index")
    public String index(QueryVO query,Model model){
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",StatusEnum.NORMAL.getId());

        if(query != null){
            if(StringUtils.isNotBlank(query.getId())){
                queryWrapper.eq("id",query.getId());
            }
            if(StringUtils.isNotBlank(query.getName())){
                queryWrapper.like("name",query.getName());
            }
            if(StringUtils.isNotBlank(query.getMobile())){
                queryWrapper.like("mobile",query.getMobile());
            }
        }

        queryWrapper.orderByDesc("last_entry_time");
        model.addAttribute("dataList", studentMapper.selectList(queryWrapper));
        model.addAttribute("query",query);
        return "/student/index";
    }

    @RequestMapping("addIndex")
    public String addIndex(){
        return "/student/add";
    }

    @RequestMapping("add")
    public String add(Model model,Student obj){
        obj.setTemperatureStatus(YesNoEnum.YES.getId());
        obj.setStatus(StatusEnum.NORMAL.getId());
        Account account = accountMapper.selectOne(new LambdaQueryWrapper<Account>().eq(Account::getMobile,obj.getMobile()));
        if(account != null){
            model.addAttribute("msg","手机号不能重复");
            return "/error";
        }
        String id = CodeUtil.genId();
        obj.setId(id);
        studentMapper.insert(obj);

        // 添加账号
        Account newAccount = new Account();
        newAccount.setName(obj.getName());
        newAccount.setCode(obj.getCode());
        newAccount.setMobile(obj.getMobile());
        newAccount.setUserType(UserTypeEnum.STUDENT.getId());
        newAccount.setPasssword(obj.getPwd());
        newAccount.setInfoId(id);
        accountMapper.insert(newAccount);

        return index(new QueryVO(),model);
    }

    @RequestMapping("del")
    public String del(String id,Model model){
        Student staff = studentMapper.selectById(id);
        staff.setStatus(StatusEnum.DELETE.getId());
        studentMapper.updateById(staff);
        accountMapper.delete(new LambdaQueryWrapper<Account>().eq(Account::getInfoId,id));
        return index(new QueryVO(),model);
    }

    @RequestMapping("editIndex")
    public String editIndex(String id,Model model){
        Student staff = studentMapper.selectById(id);
        model.addAttribute("obj",staff);
        return "/student/edit";
    }

    @RequestMapping("resetDo")
    public String resetDo(String id,Model model){
        Student staff = studentMapper.selectById(id);
        staff.setTemperatureStatus(YesNoEnum.YES.getId());
        studentMapper.updateById(staff);
        return index(new QueryVO(),model);
    }


    @RequestMapping("edit")
    public String edit(Model model,Student obj){
        studentMapper.updateById(obj);
        Account account = accountMapper.selectOne(new LambdaQueryWrapper<Account>().eq(Account::getMobile,obj.getMobile()));
        account.setPasssword(obj.getPwd());
        account.setName(obj.getName());
        account.setMobile(obj.getMobile());
        account.setCode(obj.getCode());
        accountMapper.updateById(account);
        return index(new QueryVO(),model);
    }
}
